package com.dhcc.DRGsHosp.facade.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Named;

import org.springframework.beans.factory.annotation.Autowired;

import com.dhcc.DRGsHosp.application.DepartmentApplication;
import com.dhcc.DRGsHosp.application.KeyDisGroupApplication;
import com.dhcc.DRGsHosp.application.SizerConditionApplication;
import com.dhcc.DRGsHosp.core.domain.SizerCondition;
import com.dhcc.DRGsHosp.facade.DrgsIndexCalFacade;
import com.dhcc.DRGsHosp.facade.KeyDisGroupFacade;
import com.dhcc.DRGsHosp.facade.dto.IndexCalDTO;
import com.dhcc.DRGsHosp.facade.dto.KeyDisGroupDTO;
import com.dhcc.DRGsHosp.facade.impl.assembler.IndexCalAssembler;
import com.dhcc.DRGsHosp.facade.impl.assembler.KeyDisGroupAssembler;
import com.dhcc.DRGsHosp.infra.util.DrgsIndex;
import com.dhcc.DRGsHosp.infra.util.StringUtils;

@Named("drgsHospFacadeImpl26")
public class KeyDisGroupFacadeImpl implements KeyDisGroupFacade {
	
	@Autowired
	private KeyDisGroupApplication keyDisGroupApplication;
	@Autowired
	private DepartmentApplication departmentApplication;
	@Autowired
	private DrgsIndexCalFacade drgsIndexCalFacade;
	@Autowired
	private SizerConditionApplication sizerConditionApplication;
	/**
	 * 
	 * @methodname    :getCaseYears
	 * @description   : 获取病例的年份数据
	 * @param         :
	 * @author        :mazhaoyan
	 * @return        :
	 * @date          :2016年12月16日
	 */
	@Override
	public List<Object> getCaseYears() {
		return keyDisGroupApplication.getCaseYears();
	}
	
	@Override
	public List<Object> getCaseYears(String hospCode) {
		return keyDisGroupApplication.getCaseYears(hospCode);
	}
	/**
	 * 
	 * @methodname    :setKeyDisGroup
	 * @description   : 设置重点病组
	 * @param         :
	 * @author        :mazhaoyan
	 * @return        :
	 * @date          :2016年12月16日
	 */
	@Override
	public int setKeyDisGroup(KeyDisGroupDTO keyDisGroupDTO) {
		String[] years = keyDisGroupDTO.getYears();//数据年份
		int maxYear=0;
		//获取最大年份
		if(years.length>0) {
			for(String  year : years ) {
				if(Integer.valueOf(year)>maxYear) {
					maxYear=Integer.valueOf(year);
				}
			}
		}
		String stdid=drgsIndexCalFacade.getStdID(DrgsIndex.formateDateFrom(maxYear+""),DrgsIndex.formateDateTo(maxYear+""));
		String verregion=drgsIndexCalFacade.getGroupScheme();
		return keyDisGroupApplication.setKeyDisGroup(KeyDisGroupAssembler.toEntity(keyDisGroupDTO),stdid,verregion);
	}

	
	/**
	 * 
	 * @methodname    :getKeyDisGroupDataMdc
	 * @description   :获取重点病组数据 按mdc统计
	 * @param         :
	 * @author        :mazhaoyan
	 * @return        :
	 * @date          :2016年12月15日
	 */
	@Override
	public List<Map<String, Object>> getKeyDisGroupDataMdc(String[] years,String depCode,String userCode,String hospCode) {
		int maxYear=0;
		//获取最大年份
		if(years.length>0) {
			for(String  year : years ) {
				if(Integer.valueOf(year)>maxYear) {
					maxYear=Integer.valueOf(year);
				}
			}
		}
		String stdid=drgsIndexCalFacade.getStdID(DrgsIndex.formateDateFrom(maxYear+""),DrgsIndex.formateDateTo(maxYear+""));
		String verregion=drgsIndexCalFacade.getGroupScheme();
		return keyDisGroupApplication.getKeyDisGroupDataMdc(years,depCode,userCode,stdid,verregion,hospCode);
	}
	
	/**
	 * 
	 * @methodname    :getKeyDisGroupDataDrg
	 * @description   : 获取重点病组数据 按drg统计  MDC下DRG或者全部MDC下DRG
	 * @param         :
	 * @author        :mazhaoyan
	 * @return        :
	 * @date          :2016年12月16日
	 */
	@Override
	public List<Map<String, Object>> getKeyDisGroupDataDrg(KeyDisGroupDTO keyDisGroupDTO) {
		String[] years = keyDisGroupDTO.getYears();//数据年份
		int maxYear=0;
		//获取最大年份
		if(years.length>0) {
			for(String  year : years ) {
				if(Integer.valueOf(year)>maxYear) {
					maxYear=Integer.valueOf(year);
				}
			}
		}
		String stdid=drgsIndexCalFacade.getStdID(DrgsIndex.formateDateFrom(maxYear+""),DrgsIndex.formateDateTo(maxYear+""));
		String verregion=drgsIndexCalFacade.getGroupScheme();
		return keyDisGroupApplication.getKeyDisGroupDataDrg(KeyDisGroupAssembler.toEntity(keyDisGroupDTO), stdid, verregion);
	}
	
	/**
	 * 
	 * @methodname    :getExtraDisGroup
	 * @description   : 获取重点病组以外的病组 让用户筛选
	 * @param         :
	 * @author        :mazhaoyan
	 * @return        :
	 * @date          :2016年12月17日
	 */
	@Override
	public List<Map<String, Object>> getExtraDisGroup(String[] years,String depCode,String userCode,String hospCode) {
		int maxYear=0;
		//获取最大年份
		if(years.length>0) {
			for(String  year : years ) {
				if(Integer.valueOf(year)>maxYear) {
					maxYear=Integer.valueOf(year);
				}
			}
		}
		String stdId=drgsIndexCalFacade.getStdID(DrgsIndex.formateDateFrom(maxYear+""),DrgsIndex.formateDateTo(maxYear+""));
		String verRegion=drgsIndexCalFacade.getGroupScheme();
		return keyDisGroupApplication.getExtraDisGroup(years,depCode,userCode,stdId,verRegion,hospCode);
	}
	
	/**
	 * 
	 * @methodname    :delKeyDisGroup
	 * @description   : 删除重点病组
	 * @param         :
	 * @author        :mazhaoyan
	 * @return        :
	 * @date          :2016年12月16日
	 */
	@Override
	public void delKeyDisGroup(String drgCode,String depCode,String userCode) {
		keyDisGroupApplication.delKeyDisGroup(drgCode,depCode,userCode);
	}
	
	/**
	 * 
	 * @methodname    :addKeyDisGroup
	 * @description   : 添加重点病组
	 * @param         :
	 * @author        :mazhaoyan
	 * @return        :
	 * @date          :2016年12月17日
	 */
	@Override
	public void addKeyDisGroup(String drgCode,String depCode,String userCode) {
		// 筛选条件
		if(!StringUtils.hasLength(depCode)) {
			depCode = "Hosp" ;
		}
		String confCode = "keyGroup_"+userCode+"_"+depCode;
		keyDisGroupApplication.addKeyDisGroup(drgCode,confCode);
	}
	
	/**
	 * 
	 * @methodname    :getDisGroupCollectInfo
	 * @description   : 获取病组的汇总信息
	 * @param         :
	 * @author        :mazhaoyan
	 * @return        :
	 * @date          :2016年12月19日
	 */
	@Override
	public List<Map<String,Object>> getDisGroupCollectInfo(IndexCalDTO dto) {
		String stdId = drgsIndexCalFacade.getStdID(dto.getDateFrom(),dto.getDateTo());
		dto.setStdId(stdId);
		String verRegion=drgsIndexCalFacade.getGroupScheme();
		dto.setVerRegion(verRegion);	
		List<Map<String,Object>> result = keyDisGroupApplication.getDisGroupCollectInfo(IndexCalAssembler.toEntity(dto));
		return result;
	}
	/**
	 * 
	 * @methodname    :getCaseFeeDistribute
	 * @description   : 获取病例费用分布数据
	 * @param         :
	 * @author        :mazhaoyan
	 * @return        :
	 * @date          :2016年12月23日
	 */
	@Override
	public List<Map<String,Object>> getCaseFeeDistribute(IndexCalDTO dto,int sliderValue) {
		String manageYear = drgsIndexCalFacade.getManageYear(dto);
		String stdId = drgsIndexCalFacade.getStdID(dto.getDateFrom(),dto.getDateTo());
		String verRegion=drgsIndexCalFacade.getGroupScheme();
		dto.setDateFrom(DrgsIndex.formateDateFrom(dto.getDateFrom()));
		dto.setDateTo(DrgsIndex.formateDateTo(dto.getDateTo()));
		dto.setManageYear(manageYear);
		dto.setStdId(stdId);
		dto.setVerRegion(verRegion);
		List<Map<String,Object>> result = keyDisGroupApplication.getCaseFeeDistribute(IndexCalAssembler.toEntity(dto),sliderValue);
		return result;
	}
	
	/**
	 * 
	 * @methodname    :getQuadrantTrendData
	 * @description   : 象限图趋势图
	 * @param         :
	 * @author        :mazhaoyan
	 * @return        :
	 * @date          :2016年12月23日
	 */
	@Override
	public Map<String, Object> getQuadrantTrendData(IndexCalDTO dto) {
		String mrType = dto.getMrType();//检测时间类型 MC=累计 M=月份 custom=自定义
		String dateFrom = DrgsIndex.formateDateFrom(dto.getDateFrom()).substring(0, 7) ;//开始时间
		String dateTo = DrgsIndex.formateDateTo(dto.getDateTo()).substring(0, 7);//结束时间
		String drgCode = dto.getPardrgCode();//drg代码
		String depCode = dto.getPardepCode();//科室代码
		String[] codes = dto.getQuotaCode().split("θ");//获取指标代码
		String[] deps = null;//记录科室代码
		if(StringUtils.hasLength(dto.getDeps()))
		{
			deps = dto.getDeps().split("θ");
			dto.setDeps("");//此处要将其置空
		}
		Map<String, Object> resultMap = new HashMap<String,Object>();
		if("MC".equals(mrType))
		{//累计 类型
			//查询各年累计时间内的数据
			int year =Integer.parseInt(dateFrom.substring(0,4));//年份
			for(int i = 0;i<codes.length;i++)
			{
				dto.setQuotaCode(codes[i]);
				Map<String,Object> quotaMap = new HashMap<String,Object>();
				if(StringUtils.hasLength(depCode))
				{
					dto.setPardepCode(depCode);
				}
				else
				{
					//没有限定科室
					dto.setPardepCode("");//画完平均住院日后，把科室代码清空，开始算drgs的次均费用
				}
				dto.setMonitorType("drgs");
				//先加载DRG的
				List<Map<String,Object>> drgList = new ArrayList<Map<String,Object>>();
				for(int j = year-4;j<=year;j++)
				{
					Map<String,Object> yearMap = new HashMap<String,Object>();
					dto.setDateFrom(j+dateFrom.substring(4));//开始时间
					dto.setDateTo(j+dateTo.substring(4));//结束时间
					yearMap.put("ddesc",j+"年");
					List<Map<String, Object>> tmpData =  (List<Map<String, Object>>) drgsIndexCalFacade.indexCal(dto);
					if(!tmpData.isEmpty())
					{
						yearMap.put("QuotaValue",tmpData.get(0).get("QuotaValue"));
					}
					else
					{
						yearMap.put("QuotaValue",null);
					}
					drgList.add(yearMap);
				}
				quotaMap.put(drgCode,drgList);
				if(deps!=null)
				{
					dto.setMonitorType("dep");
					for(int j = 0;j<deps.length;j++)
					{
						String depName = departmentApplication.findDepNameByCode(deps[j]);
						dto.setPardepCode(deps[j]);
						List<Map<String,Object>> drgDepList = new ArrayList<Map<String,Object>>();
						for(int k = year-4;k<=year;k++)
						{
							Map<String,Object> yearMap = new HashMap<String,Object>();
							dto.setDateFrom(k+dateFrom.substring(4));//开始时间
							dto.setDateTo(k+dateTo.substring(4));//结束时间
							yearMap.put("ddesc",k+"年");
							List<Map<String, Object>> tmpData =  (List<Map<String, Object>>) drgsIndexCalFacade.indexCal(dto) ;
							if(!tmpData.isEmpty())
							{
								yearMap.put("QuotaValue",tmpData.get(0).get("QuotaValue"));
							}
							else
							{
								yearMap.put("QuotaValue",null);
							}
							drgDepList.add(yearMap);
						}
						quotaMap.put(depName,drgDepList);
					}
				}
				resultMap.put(codes[i],quotaMap);
			}
		}
		else if("M".equals(mrType))
		{//月份 类型
			//查询本年各月数据
			int month =Integer.parseInt(dateTo.substring(5));//月份
			String year = dateTo.substring(0,4);//年份
			for(int i = 0;i<codes.length;i++)
			{
				dto.setQuotaCode(codes[i]);
				Map<String,Object> quotaMap = new HashMap<String,Object>();
				if(StringUtils.hasLength(depCode))
				{
					dto.setPardepCode(depCode);
				}
				else
				{
					//没有限定科室
					dto.setPardepCode("");//画完平均住院日后，把科室代码清空，开始算drgs的次均费用
				}
				dto.setMonitorType("drgs");
				//先加载DRG的
				List<Map<String,Object>> drgList = new ArrayList<Map<String,Object>>();
				for(int j = 1;j<=month;j++)
				{
					Map<String,Object> monthMap = new HashMap<String,Object>();
					if(j<10)
					{
						dto.setDateFrom(year+"-0"+j);//开始时间
						dto.setDateTo(year+"-0"+j);//结束时间
						monthMap.put("ddesc",year+"年0"+j+"月");
					}
					else
					{
						dto.setDateFrom(year+"-"+j);//开始时间
						dto.setDateTo(year+"-"+j);//结束时间
						monthMap.put("ddesc",year+"年"+j+"月");
					}
					List<Map<String, Object>> tmpData =  (List<Map<String, Object>>) drgsIndexCalFacade.indexCal(dto);
					if(!tmpData.isEmpty())
					{
						monthMap.put("QuotaValue",tmpData.get(0).get("QuotaValue"));
					}
					else
					{
						monthMap.put("QuotaValue",null);
					}
					drgList.add(monthMap);
				}
				quotaMap.put(drgCode,drgList);
			
				if(deps!=null)
				{
					dto.setMonitorType("dep");
					for(int j = 0;j<deps.length;j++)
					{
						String depName = departmentApplication.findDepNameByCode(deps[j]);
						dto.setPardepCode(deps[j]);
						List<Map<String,Object>> drgDepList = new ArrayList<Map<String,Object>>();
						for(int k = 1;k<=month;k++)
						{
							Map<String,Object> monthMap = new HashMap<String,Object>();
							if(k<10)
							{
								dto.setDateFrom(year+"-0"+k);//开始时间
								dto.setDateTo(year+"-0"+k);//结束时间
								monthMap.put("ddesc",year+"年0"+k+"月");
							}
							else
							{
								dto.setDateFrom(year+"-"+k);//开始时间
								dto.setDateTo(year+"-"+k);//结束时间
								monthMap.put("ddesc",year+"年"+k+"月");
							}
							List<Map<String, Object>> tmpData =  (List<Map<String, Object>>) drgsIndexCalFacade.indexCal(dto);
							if(!tmpData.isEmpty())
							{
								monthMap.put("QuotaValue",tmpData.get(0).get("QuotaValue"));
							}
							else
							{
								monthMap.put("QuotaValue",null);
							}
							drgDepList.add(monthMap);
						}
						quotaMap.put(depName,drgDepList);
					}
				}
				resultMap.put(codes[i],quotaMap);
			}
		}else if("custom".equals(dto.getMrType())){
			//自定义 类型
			int yearFrom = Integer.parseInt(dateFrom.substring(0, 4));
			int yearTo = Integer.parseInt(dateTo.substring(0, 4));
			for(int i = 0;i<codes.length;i++)
			{
				dto.setQuotaCode(codes[i]);
				Map<String,Object> quotaMap = new HashMap<String,Object>();
				if(StringUtils.hasLength(depCode))
				{
					dto.setPardepCode(depCode);
				}
				else
				{
					//没有限定科室
					dto.setPardepCode("");//画完平均住院日后，把科室代码清空，开始算drgs的次均费用
				}
				dto.setMonitorType("drgs");
				//先加载DRG的
				List<Map<String,Object>> drgList = new ArrayList<Map<String,Object>>();
				for(int m=4; m>=0;m--)
				{
					Map<String,Object> yearMap = new HashMap<String,Object>();
					String reldateFrom = (yearFrom-m)+"-"+dateFrom.substring(5,7);
					String reldateTo = (yearTo-m)+"-"+dateTo.substring(5,7);
					dto.setDateFrom(reldateFrom);
					dto.setDateTo(reldateTo);
					yearMap.put("ddesc", reldateFrom.replace("-",".")+"~"+reldateTo.replace("-","."));
					List<Map<String, Object>> tmpData =  (List<Map<String, Object>>) drgsIndexCalFacade.indexCal(dto);
					if(!tmpData.isEmpty())
					{
						yearMap.put("QuotaValue",tmpData.get(0).get("QuotaValue"));
					}
					else
					{
						yearMap.put("QuotaValue",null);
					}
					drgList.add(yearMap);
				}
				quotaMap.put(drgCode,drgList);
				
				if(deps!=null)
				{
					dto.setMonitorType("dep");
					for(int j = 0;j<deps.length;j++)
					{
						String depName = departmentApplication.findDepNameByCode(deps[j]);
						dto.setPardepCode(deps[j]);
						List<Map<String,Object>> drgDepList = new ArrayList<Map<String,Object>>();
						for(int m=4; m>=0;m--)
						{
							Map<String,Object> yearMap = new HashMap<String,Object>();
							String reldateFrom = (yearFrom-m)+"-"+dateFrom.substring(5,7);
							String reldateTo = (yearTo-m)+"-"+dateTo.substring(5,7);
							dto.setDateFrom(reldateFrom);
							dto.setDateTo(reldateTo);
							yearMap.put("ddesc", reldateFrom.replace("-",".")+"~"+reldateTo.replace("-","."));
							List<Map<String, Object>> tmpData =  (List<Map<String, Object>>) drgsIndexCalFacade.indexCal(dto) ;
							if(!tmpData.isEmpty())
							{
								yearMap.put("QuotaValue",tmpData.get(0).get("QuotaValue"));
							}
							else
							{
								yearMap.put("QuotaValue",null);
							}
							drgDepList.add(yearMap);
						}
						quotaMap.put(depName,drgDepList);
					}
				}
				resultMap.put(codes[i],quotaMap);
			}
		}
		return resultMap;
	}

	/**  
	 *  @Description：
	 *  @Author mazhaoyan  
	 *  @see com.dhcc.DRGsHosp.facade.KeyDisGroupFacade#findSizerCondition(java.lang.String)  
	 */
	@Override
	public String findSizerCondition(String moudkeCode) {
		SizerCondition sc = new SizerCondition();
		sc = sizerConditionApplication.findSizerCondition(moudkeCode);
		String config = "";
		if(sc != null){
			config = sc.getConditionValue();
		}
		return config;
	}

}
